# Maintainer: psykose <alice@ayaya.dev>
pkgname=electron
pkgver=19.0.8
pkgrel=0
_gitrev=c67ca40ed6054aadcdfb901aa1abaee2ccc690f3
_chromium=102.0.5005.148
_depot_tools=9b9f451addeb3ad230e9385d5e2c20cd1632c491
pkgdesc="Electron cross-platform desktop toolkit"
url="https://github.com/electron/electron"
arch="aarch64 x86_64" # same as chromium
license="MIT"
depends="gtk+3.0 so:libudev.so.1 xdg-utils"
makedepends="
	alsa-lib-dev
	bash
	bsd-compat-headers
	bzip2-dev
	c-ares-dev
	cairo-dev
	clang-dev
	clang-extra-tools
	cups-dev
	curl-dev
	dbus-glib-dev
	eudev-dev
	ffmpeg-dev
	findutils
	flac-dev
	flex
	freetype-dev
	gperf
	gtk+3.0-dev
	gn
	gzip
	harfbuzz-dev
	hunspell-dev
	http-parser-dev
	hwids-usb
	java-jdk
	jpeg-dev
	jsoncpp-dev
	krb5-dev
	lcms2-dev
	libarchive-tools
	libbsd-dev
	libcap-dev
	libevent-dev
	libexif-dev
	libgcrypt-dev
	libgnome-keyring-dev
	libjpeg-turbo-dev
	libnotify-dev
	libpng-dev
	libusb-dev
	libva-dev
	libwebp-dev
	libxcomposite-dev
	libxcursor-dev
	libxinerama-dev
	libxml2-dev
	libxrandr-dev
	libxscrnsaver-dev
	libxslt-dev
	linux-headers
	lld
	mesa-dev
	nghttp2-dev
	nodejs
	npm
	nss-dev
	opus-dev
	pciutils-dev
	perl
	pipewire-dev
	pulseaudio-dev
	py3-httplib2
	py3-parsing
	py3-six
	python3
	pythonispython3
	re2-dev
	samurai
	snappy-dev
	speex-dev
	sqlite-dev
	xcb-proto
	yarn
	zlib-dev
	"
subpackages="$pkgname-lang"
# the lower patches are specific to electron, the top ones are from the equivalent chromium version
source="https://dev.alpinelinux.org/archive/electron/electron-$pkgver.tar.xz
	chromium-102-fenced_frame_utils-include.patch
	chromium-102-regex_pattern-array.patch
	chromium-VirtualCursor-standard-layout.patch
	chromium-revert-drop-of-system-java.patch
	chromium-use-alpine-target.patch
	credentials-header.patch
	default-pthread-stacksize.patch
	elf-arm.patch
	fix-missing-cstdint-include-musl.patch
	fix-narrowing-cast.patch
	fix-unittests-sandbox.patch
	gdbinit.patch
	iwyu-add-utility-for-std-exchange.patch
	libxml-unbundle.patch
	media-base.patch
	memory-tagging-arm64.patch
	musl-crashpad.patch
	musl-fixes.patch
	musl-hacks.patch
	musl-libc++.patch
	musl-sandbox.patch
	musl-stat.patch
	musl-tid-caching.patch
	musl-v8-monotonic-pthread-cont_timedwait.patch
	nasm.patch
	no-execinfo.patch
	no-getcontext.patch
	no-mallinfo.patch
	python-jinja-3.10.patch
	quiche-arena-size.patch
	remove-no-opaque-pointers-flag.patch
	remove-strip_binary.patch
	resolver.patch
	revert-use-ffile-compilation-dir.patch
	roll-src-third_party-ffmpeg.patch

	scoped-file.patch
	system-node.patch
	vector-const.patch
	"
options="!check suid"

# clang uses much less memory (and this doesn't support gcc)
export CC=clang
export CXX=clang++
# required to find the tools
export AR=ar
export NM=nm
export LD=clang
# less log spam
export CFLAGS="$CFLAGS -O2 -Wno-unknown-warning-option"
export CXXFLAGS="$CXXFLAGS -O2 -Wno-unknown-warning-option"

_gn_flags() {
	echo "$@"
}

# creates a dist tarball that does not need to git clone everything at build time.
snapshot() {
	export CHROMIUM_BUILDTOOLS_PATH="$srcdir/src/buildtools"
	mkdir -p "$srcdir"
	cd "$srcdir"
	git clone --branch=$_chromium --depth=1 \
		https://chromium.googlesource.com/chromium/src.git

	git clone https://github.com/electron/electron.git

	(
		git clone --depth 1 -b main https://chromium.googlesource.com/chromium/tools/depot_tools.git
		cd depot_tools
		git fetch --depth 1 origin $_depot_tools
		git checkout $_depot_tools
	)

	export PATH="$PATH:$srcdir/depot_tools"

	echo "solutions = [
	{
		\"name\": \"src/electron\",
		\"url\": \"file://$srcdir/electron@$_gitrev\",
		\"deps_file\": \"DEPS\",
		\"managed\": False,
		\"custom_deps\": {
			\"src\": None,
		},
		\"custom_vars\": {},
	},
	]" > .gclient

	python3 depot_tools/gclient.py sync \
		--with_branch_heads \
		--with_tags \
		--nohooks

	python3 src/build/landmines.py
	python3 src/build/util/lastchange.py -o src/build/util/LASTCHANGE
	python3 src/build/util/lastchange.py -m GPU_LISTS_VERSION \
		--revision-id-only --header src/gpu/config/gpu_lists_version.h
	python3 src/build/util/lastchange.py -m SKIA_COMMIT_HASH \
		-s src/third_party/skia --header src/skia/ext/skia_commit_hash.h
	python3 electron/script/apply_all_patches.py \
		electron/patches/config.json
	python3 src/tools/download_optimization_profile.py \
		--newest_state=src/chrome/android/profiles/newest.txt \
		--local_state=src/chrome/android/profiles/local.txt \
		--output_name=src/chrome/android/profiles/afdo.prof \
		--gs_url_base=chromeos-prebuilt/afdo-job/llvm

	find src \( -name ".git" -o -name ".gitignore" -o -name ".gitmodules" -o -name ".gitattributes" \) \
		-exec rm -rf -- {} +
	mv src $pkgname-$pkgver
	msg "generating tarball.. (this takes over three hours.)"
	tar -cf $pkgname-$pkgver.tar $pkgname-$pkgver
	xz -vv -e -9 $pkgname-$pkgver.tar
}

prepare() {
	default_prepare

	git init .

	# link to system tools
	ln -sfv /usr/bin/clang-format buildtools/linux64/clang-format
	mkdir -p third_party/node/linux/node-linux-x64/bin
	ln -sfv /usr/bin/node third_party/node/linux/node-linux-x64/bin/node
	ln -sfv /usr/bin/java third_party/jdk/current/bin/java

	(
		cd electron
		git init .
		yarn install --frozen-lockfile
	)

	(
		cd third_party/node
		npm ci
	)

	local use_system="
		ffmpeg
		flac
		fontconfig
		freetype
		harfbuzz-ng
		libdrm
		libevent
		libjpeg
		libpng
		libwebp
		libxml
		libxslt
		opus
		re2
		snappy
		"

	for _lib in $use_system libjpeg_turbo; do
		msg "Removing buildscripts for system provided $_lib"
		find . -type f -path "*third_party/$_lib/*" \
			\! -path "*third_party/$_lib/chromium/*" \
			\! -path "*third_party/$_lib/google/*" \
			\! -path './third_party/libxml/*' \
			\! -path './third_party/pdfium/third_party/freetype/include/pstables.h' \
			\! -path './third_party/harfbuzz-ng/utils/hb_scoped.h' \
			\! -regex '.*\.\(gn\|gni\|isolate\|py\)' \
			-delete
	done


	# https://groups.google.com/a/chromium.org/d/topic/chromium-packagers/9JX1N2nf4PU/discussion
	touch chrome/test/data/webui/i18n_process_css_test.html
	# Use the file at run time instead of effectively compiling it in
	sed 's|//third_party/usb_ids/usb.ids|/usr/share/hwdata/usb.ids|g' \
		-i services/device/public/cpp/usb/BUILD.gn

	python3 build/linux/unbundle/replace_gn_files.py --system-libraries \
		$use_system
	python3 third_party/libaddressinput/chromium/tools/update-strings.py

	# prevent annoying errors when regenerating gni
	sed -i 's,^update_readme$,#update_readme,' \
		third_party/libvpx/generate_gni.sh

	# allow system dependencies in "official builds"
	sed -i 's/OFFICIAL_BUILD/GOOGLE_CHROME_BUILD/' \
		tools/generate_shim_headers/generate_shim_headers.py

	_gn_args=$(_gn_flags \
		import\(\"//electron/build/args/release.gn\"\) \
		custom_toolchain=\"//build/toolchain/linux/unbundle:default\" \
		host_toolchain=\"//build/toolchain/linux/unbundle:default\" \
		blink_symbol_level=0 \
		build_with_tflite_lib=false \
		chrome_pgo_phase=0 \
		clang_use_chrome_plugins=false \
		disable_fieldtrial_testing_config=true \
		enable_hangout_services_extension=true \
		enable_nacl=false \
		enable_precompiled_headers=false \
		enable_vr=false \
		fatal_linker_warnings=false \
		ffmpeg_branding=\"Chrome\" \
		icu_use_data_file=true \
		is_cfi=false \
		is_clang=true \
		is_component_build=false \
		is_debug=false \
		is_official_build=true \
		link_pulseaudio=true \
		proprietary_codecs=true \
		rtc_use_pipewire=true \
		symbol_level=0 \
		treat_warnings_as_errors=false \
		use_allocator=\"none\" \
		use_allocator_shim=false \
		use_cfi_icall=false \
		use_cups=true \
		use_custom_libcxx=false \
		use_gnome_keyring=false \
		use_gold=false \
		use_lld=true \
		use_pulseaudio=true \
		use_sysroot=false \
		use_system_freetype=true \
		use_system_harfbuzz=true \
		use_system_lcms2=true \
		use_system_libdrm=true \
		use_system_libjpeg=true \
		use_system_libpng=true \
		use_system_zlib=true \
		use_thin_lto=false \
		use_vaapi=true \
		)

	gn gen out/Release --args="$_gn_args"
}

build() {
	ninja -C out/Release electron_dist_zip
}

package() {
	mkdir -p "$pkgdir"/usr/lib/electron "$pkgdir"/usr/bin

	bsdtar -xf out/Release/dist.zip -C "$pkgdir"/usr/lib/electron

	chmod u+s "$pkgdir"/usr/lib/electron/chrome-sandbox

	ln -sfv /usr/lib/electron/electron "$pkgdir"/usr/bin/electron
}

lang() {
	pkgdesc="$pkgdesc (translations)"
	install_if="$pkgname=$pkgver-r$pkgrel lang"

	mkdir -p "$subpkgdir"/usr/lib/electron/locales

	mv "$pkgdir"/usr/lib/electron/locales/*.pak \
		"$subpkgdir"/usr/lib/electron/locales
	mv "$subpkgdir"/usr/lib/electron/locales/en-US.pak \
		"$pkgdir"/usr/lib/electron/locales
}

sha512sums="
230dbce925c4561cba90fbd9c7257a743436956688174e5dc55f12ee3bb6047577b6d215eca67d1f781fb7e3be3dbccf0f9da050e255fadc64692e1c9ab0853f  electron-19.0.8.tar.xz
459910ac11dfd2953240a72836aac55c33d914906194897b38328f482173a101930cad1656906df0bc1c903d41199eee8089f474f5d8b056cfb12c8f2b0acffb  chromium-102-fenced_frame_utils-include.patch
a6f066e0cbb4e0644733b48013f150647b3c431dc5ebcbff611b3214c86739726208874f67cb339140045e7934e7636f7514bd7aec603545d47c7ab6ca205be4  chromium-102-regex_pattern-array.patch
0fc131ef4ad32964815744080f40cc2566c70da1cd4ddaf1da8456d1c86e67e3ebf38f43208d17d40b646744e8e662f5b314e236a1c90bc6f8b936313e42a3cf  chromium-VirtualCursor-standard-layout.patch
c4654d5b23c6f5d9502507e534fe1951d6749c62251e49b6adfe10d1569431e7f7a5a6fa5ff09ec30984415ced27a5e20985df8c91295de34af3c84557fa5b91  chromium-revert-drop-of-system-java.patch
ded02360a36283973b9abb4150de3d964749a58017fbbd9f09fb6cb29ec97a8ac99ccadd953633bbb7eb9ec77a078f4df9de5f624d1580f33446ceba5fd3a3f6  chromium-use-alpine-target.patch
274858323d040ac8c51bac90b6ef91bb075d7b8d92d73952ed700c10a8bae2c2115fb2a9cc6912de79be226c141d7106839fc3486e22c0206e75cb6d8ff65ee6  credentials-header.patch
22da17c649ae9845d6a174187f2b666245a10db8a997169fc214c430fbba10143cfb57f8f6ece5acf856cd9ea09f293a1aca90cdf3e64fd9423b78beac53f517  default-pthread-stacksize.patch
9b1441fe42421ef9ead86a5143bf997d989bd53a8969cdfdf4e0c45f847efc8c1d127e15fb05e2ef5dbefbb228cbdc2849404d74997ca56265599e12e5efd6e4  elf-arm.patch
8bebf4a9d5225c6e47edc5b07c4b97be24a45cc221f49632836915ceeb4ecb69b7f79a31ea7f82171cde3443f45fec541f409892542cf1014e81aa6acd01566d  fix-missing-cstdint-include-musl.patch
e60f381c557a15598d71e33b7aacc63be4a8e17b16cdd95047df5e5e9577ad925864ffd64597d5dbc9a4516de13b3f17c26099b36692165df8c66b1b4d399ce9  fix-narrowing-cast.patch
54f07ee9d85577e284e04460e79132fa6769251ceb045561cea811dde0369e9e4cdbcf26566235b0d12c649be4d46fea294f7c6a4e2a75109dc3282e6e7d7993  fix-unittests-sandbox.patch
82f2bd4aa613e792d3af6e7a8632e0920cf48504a7572fb97d5480ea182fa125fe526e7df1ce4311b5d139a3f78da0f0b54605fbd09d7cd43135906a7639e47f  gdbinit.patch
4d268992ddb341e70f4e730c82c676c0c95b3958bc9553bbe83383cebde7373c79a8e9aeef47c7af2add8353263b02ccfd7e5bd864f544a584361a02d30374ec  iwyu-add-utility-for-std-exchange.patch
17b54042c58cd448c7e26dca1504bb48d85fdd7c2385930e639c283493a5e430923041f01cde5a5dfaa32fbb7f0a645baca55b202d56d3f245fb4febdf026da2  libxml-unbundle.patch
599f5ef338b5797e04436bdc88921c31365d2dea2613d40053be6e774f654e2889efb0c0bd55c2c5044a6805510fbc337da555971169a75d584fac93e18256ac  media-base.patch
63e5b25c49af484964b3ac723da1c2f348812572a2e24ad412d2fb9eb7eec9ddc0e76568e76d1f352cdc562bc5003c93e04ee519a82e8568f48484d1682e237f  memory-tagging-arm64.patch
2e5a83826239849d096aead033eb519338b84e6fbbf58abfcd7cfe40d0b3070b66bcc825ec84406484d73d1a333f1b9d23a8ab44987ab5c8b187076c84cd668b  musl-crashpad.patch
9fbfeb1f52e48658f46e55db29de02b554f24ae79f46bbd89bbf05c20f538c9bb3c8624a6e27907d13d6717e5f55a332b13577d8672539662c90c9a10109b94a  musl-fixes.patch
20b6931937ae792ddf4e1be7f0cbf0f0931b0afce9f6528b7ae5edb1bb2db070438caa8edc94e915e1e5ab1a191789129877d0353fe8976f537df3775dd3ab5d  musl-hacks.patch
a361989c7ff4a285ed36c499e4fe1e97ed8d904a9b92f8d3c01b056b469490b7d215b6edb803ba3b0955633f58f3682d4fb60cc710cba68153a6b23df3ba7ba9  musl-libc++.patch
ef43fa90b05e2407b6daa822193121cdeef6ee9068175572f0928f156f186be9c77539dfa06863adfaa25afd498ec1b5081f9e476543616f9472b9033fd921dc  musl-sandbox.patch
0c0d3ed0a8f2bcecb2bf4eb56130fcd71031bb63744fb56e1b5a6b9dfbe16680607ff592e24602fe045b819509a70556e240b6fe0e876710977d60185f682df4  musl-stat.patch
a018cf58b9e34f73d588961f2f9a36de67267060fb2e77ebc562974c6c92a59009161e374cf9bbdc35e8bf1a7e90f0b90117a0d494dcd2e2bb2f5e6534925d0b  musl-tid-caching.patch
556cc2ba28184a75745f13c673605327f5be590be179eb3f941dc0abded854a814e5da1432a28fb8d4dcdcd66ffd4c94ee1a8824d2cd93b632f48e9310dbc3fa  musl-v8-monotonic-pthread-cont_timedwait.patch
7957aae04e8ef3badbc98c982ba1acb1b9ef5e665440116c704512f11554d7b64ba3643b7ff7d00476a6686324033b0cf762deb78ce798619226380953b9dc9c  nasm.patch
7aa351e79c29de63972f1f16fbab0465b2569bc0a22174780988e0cdfe4fcaff3c664dd277937bb33f2b77f108d20afa8720c427524b09d26074f9cf6807e147  no-execinfo.patch
e8675aff9fccd8b58cc839ef1d9e0e022d66aa94f72baa72e7acf92ceb2691f6a7907cc60ec3a87f894efdd639a3dec7fe702c97d446fcdea99452caec57e002  no-getcontext.patch
c741fafab758a675f7007a450df7d9acfe05149f4ce6dd4cf82dc6e97ddcd6b5b4a6b8e09820c2ad3ea62487f30b34d848739908e95ca8a31ca5baeb7cb9d0d7  no-mallinfo.patch
e05180199ee1d559e4e577cedd3e589844ecf40d98a86321bf1bea5607b02eeb5feb486deddae40e1005b644550331f6b8500177aa7e79bcb3750d3c1ceb76c3  python-jinja-3.10.patch
c7fc8a1a079224b9ed6a1f8331cef901833bec04f337fa122381e0a65a1acdd529368f6653dc6d03475381659e07e6e31dd2ec0530257606480f06d15faf3aaf  quiche-arena-size.patch
c8e892e36e7292cd6db36017fff994a0fbc00dd83ea9010422c21cd56da89c9eddd1712e4ff4b3fec354fa595ce3cc403b96fc13525e74fe6538461d5c2d9b50  remove-no-opaque-pointers-flag.patch
91dafba2138175b6618ec4c96c7ea39f3a26f6a09b69adabad4f9355dd84d436f3de3274b31465d5f46deeb452fde64e4cfbbda0bb4ed7e9a25d953e1b73e5d2  remove-strip_binary.patch
cd7719c30518aeb659e9b7dc126241f897afe1a231e601271b63ba170e811a93b514c4d2a77680cdf0c846f81fa6db766ac619aa200efb32cbdfff6732a63893  resolver.patch
2fdd0b2fd403dd4440533288f7e08bb8480702c42c7571ea764aac18ba5eaf72494793c77e4244991d12f45e5acfceae0772995b11d323916c07cb53c43a2f59  revert-use-ffile-compilation-dir.patch
128ec0fd14349e065c8bb0910d53cbea7423182a06e06f7b7765f3cba1e5ba5e7a8bccbcdab079335b5235abb7bf0d46dee21ecc8c221be7e1c5c6d9795f958e  roll-src-third_party-ffmpeg.patch
8d11b2ba811fdd6280784bfafcef073238324941b4c33e7c9a5ccfde8506242d06e5136d697f047bab3838b43a666f1da404af7b4f4ff9a0c6bf6bdb8448e5b2  scoped-file.patch
26a8e4040e69f335a7104f42d012b9d933a40985b33a7be02add27a801c097c5a2be4c6e69faf9175ce8945210ae4c5592ecad2123ccff2beee5473194a765e3  system-node.patch
71571b15cf8bd6259b7fd22bea0e46b64890f3db776365de33fe539f26ce9ef99459e05c3dde9434c3657225bc67160abc915acd93033cb487c770c6a2a5975f  vector-const.patch
"
